package controllers

import (
	"day01/models/mysql"
	"github.com/gin-gonic/gin"
	"net/http"
)

func Register(c *gin.Context) {

	username := c.PostForm("user_name")
	password := c.PostForm("password")
	user := mysql.User{
		UserName: username,
		Password: password,
	}
	if username == "" || password == "" {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "手机号或密码不能为空",
		})
		return
	}

	if err := mysql.DB.Create(&user).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"error": err.Error(),
		})
		return
	}
	c.JSON(http.StatusCreated, gin.H{
		"message": "注册成功",
		"userId":  user.ID,
	})

}
